package me.project.jobster.controller;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.BeanUtils;
import me.project.jobster.dao.mybatis.model.Position;
import me.project.jobster.service.JobService;
import me.project.jobster.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author mbryce
 * @date 2018/4/27
 */
@Controller
@RequestMapping("jobs")
public class JobController extends BaseController {

    @Autowired
    private JobService jobService;
    @Autowired
    private MessageService messageService;

    @GetMapping("index")
    public String jobInformation(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "3");

        List<Position> list = jobService.getJobList();
        model.addAttribute("positionList", list);
        model.addAttribute("position", new Position());
        return "jobs/jobs";
    }

    /**
     * job detail page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("detail")
    public String detail(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "3");
        String pId = request.getParameter("pId");

        Position position = jobService.getJobInfo(pId);
        model.addAttribute("position", position);
        return "jobs/detail";
    }

    /**
     * job search
     *
     * @param request
     * @param model
     * @return
     */
    @PostMapping("search")
    public String search(HttpServletRequest request, Model model) throws InvocationTargetException, IllegalAccessException {
        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "3");
        String pId = request.getParameter("pId");

        Position position = new Position();
        BeanUtils.copyMap2Bean(position, getParameterMap(request));
        String gpaSearchFlag = request.getParameter("gpaSearchFlag");
        List<Position> list = jobService.getJobListFuzzy(position);
        model.addAttribute("positionList", list);
        model.addAttribute("position", position);
        return "jobs/jobs";
    }

    /**
     * post job page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/post/index")
    public String postJobPage(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "4");
        return "jobs/job_post";
    }

    @PostMapping("post")
    public String post(HttpServletRequest request, Model model) throws InvocationTargetException, IllegalAccessException {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "4");

        String userId = String.valueOf(session.getAttribute("user_id"));
        String userName = String.valueOf(session.getAttribute("user_name"));

        Position position = new Position();
        BeanUtils.copyMap2Bean(position, getParameterMap(request));
        position.setcId(userId);
        jobService.postJob(position);

        messageService.groupMessage(userId, userName, position.getpId());
        model.addAttribute("postMsg", "Post A Job Successfully!");
        return "jobs/job_post";

    }
}
